package com.xbjtech.util;

import java.util.Iterator;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.xbjtech.FailSendException;

public class SmsSendImpl implements SmsSend {

	public static final String TO_CAROWNER_FTL = "【蓝领安装】验证码${code}，尊敬的车主，您的订单已安排到如下安装店：${name}，预约电话：${phone}，地址：${address}。请提前一天致电安装店预约，完工后请自行检查安装情况。【修补匠科技有限公司】";
    public static final String TO_STORE_FTL = "【蓝领安装】订单号：${orderId}，已指派到您的门店：${orderInfo}，完工后在微信公众号“蓝领安装”提交验证码申请完工。【修补匠科技有限公司】";
	

    public void send(String smsModelId, String mobiles, Map<String, String> model) throws FailSendException {
    	HttpClientUtil client = HttpClientUtil.getInstance();
    	 String Key = "618a3e2e1e486f19c07b";
    	 String Uid = "601101548@qq.com";
		// UTF发送
		if ("1".equals(smsModelId)) {
			
			int result = client.sendMsgUtf8(Uid, Key, replaceSmsBody(TO_CAROWNER_FTL,model), mobiles);
			if (result > 0) {
				System.out.println("UTF8成功发送条数==" + result);
			} else {
				System.out.println(client.getErrorMsg(result));
			}
		}

		if ("2".equals(smsModelId)) {
			int result = client.sendMsgUtf8(Uid, Key,replaceSmsBody(TO_STORE_FTL,model) , mobiles);
			if (result > 0) {
				System.out.println("UTF8成功发送条数==" + result);
			} else {
				System.out.println(client.getErrorMsg(result));
			}
		}
    }

    public static String replaceSmsBody(String smsBody, Map<String, String> params) {
        Iterator<String> it = params.keySet().iterator();
        while (it.hasNext()) {
            String _key = it.next();
            String _value = params.get(_key);
            smsBody = smsBody.replace(("${" + _key + "}"), StringUtils.trimWhitespace(_value));
        }
        return smsBody;
    }

}
